package com.macro.service.impl;



import com.macro.enums.AppHttpCodeEnum;
import com.macro.mapper.AttributeCategoryRelationMapper;
import com.macro.mapper.CategoryMapper;
import com.macro.mapper.GoodsAttributeCategoryMapper;
import com.macro.mapper.GoodsAttributeMapper;

import com.macro.pojos.dto.PageRequestDto;
import com.macro.pojos.dto.ResponseResult;
import com.macro.pojos.pojo.AttributeCategoryRelation;
import com.macro.pojos.pojo.Category;
import com.macro.pojos.pojo.UiGoodsAttribute;
import com.macro.pojos.pojo.UiGoodsAttributeCategory;
import com.macro.pojos.pojo.Vo.CategoryParent;
import com.macro.pojos.pojo.Vo.CategoryUpdateVo;
import com.macro.pojos.pojo.Vo.CategoryVo;
import com.macro.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    GoodsAttributeCategoryMapper goodsAttributeCategoryMapper;
    @Autowired
    GoodsAttributeCategoryMapper attributeCategoryMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    AttributeCategoryRelationMapper attributeCategoryRelationMapper;
    @Override
    public ResponseResult findPageAll(PageRequestDto pageRequestDto) {
     List<Category> list= categoryMapper.findPageAll(pageRequestDto);
        return ResponseResult.okResult(list);
    }

    /**
     * 添加分类
     * @param
     * @return
     */
    @Override
    public ResponseResult addCategory(CategoryVo categoryVo) {
        if (categoryVo==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Category category = new Category();
        category.setCreateTime(new Date());
        category.setIsDelete(false);
        category.setIsMenu(0);
        category.setIsShow(0);
        category.setUpdateTime(new Date());
        BeanUtils.copyProperties(categoryVo,category);
        //属性添加
        Integer[] attributeCategoryId = categoryVo.getAttributeCategoryId();
        Integer[] attributeId = categoryVo.getAttributeId();
        AttributeCategoryRelation attributeCategoryRelation = new AttributeCategoryRelation();
        for (Integer id : attributeCategoryId) {
            List<UiGoodsAttribute> list  =  goodsAttributeMapper.findByid(id);
            for (Integer aid : attributeId) {
                for (UiGoodsAttribute goodsAttribute : list) {
                    Integer goodsAttributeId = goodsAttribute.getId().intValue();
                    if (goodsAttributeId==aid){
                        attributeCategoryRelation.setGoodsAttributeCategoryId(id);
                        attributeCategoryRelation.setGoodsAttributeId(aid);
                        attributeCategoryRelationMapper.insert(attributeCategoryRelation) ;
                    }
                }
            }

        }


        int i = categoryMapper.addCategory(category);
        if (i>0){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    /**
     * 修改分类
     * @param
     * @return
     */
    @Override
    public ResponseResult UpdateCategory(CategoryUpdateVo categoryUpdateVo) {
        if (categoryUpdateVo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Category category = new Category();
        category.setUpdateTime(new Date());
        BeanUtils.copyProperties(categoryUpdateVo,category);
        List<AttributeCategoryRelation> attributeCategoryRelations = attributeCategoryRelationMapper.selectList(null);
        Integer[] attributeCategoryId = categoryUpdateVo.getAttributeCategoryId();
        Integer[] attributeId = categoryUpdateVo.getAttributeId();
        for (AttributeCategoryRelation attributeCategoryRelation : attributeCategoryRelations) {
            AttributeCategoryRelation Relation = new AttributeCategoryRelation();
            for (Integer id : attributeCategoryId) {
                if (id==attributeCategoryRelation.getGoodsAttributeCategoryId()){
                    attributeCategoryRelationMapper.deleteRelationById(id);
                }
            }
                for (Integer cid : attributeCategoryId) {
                    List<UiGoodsAttribute> list  =  goodsAttributeMapper.findByid(cid);
                    for (Integer aid : attributeId) {
                        for (UiGoodsAttribute goodsAttribute : list) {
                            Integer goodsAttributeId = goodsAttribute.getId().intValue();
                            if (goodsAttributeId==aid){
                                attributeCategoryRelation.setGoodsAttributeCategoryId(cid);
                                attributeCategoryRelation.setGoodsAttributeId(aid);
                                attributeCategoryRelationMapper.insert(attributeCategoryRelation) ;
                            }
                        }
                    }

                }






        }


        categoryMapper.UpdateCategory(category);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult updateMenuByid(Integer id, Integer menu) {
        if (id==null || menu==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //传过来的导航状态要么为1要么为0
        if (menu==1){
            categoryMapper.updateMenuByid(id,0);
        }
        if (menu==0){
            categoryMapper.updateMenuByid(id,1);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateShowByid(Integer id, Integer show) {
        if (id==null || show==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //传过来的导航状态要么为1要么为0
        if (show==1){
            categoryMapper.updateShowByid(id,0);
        }
        if (show==0){
            categoryMapper.updateShowByid(id,1);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findListByParentId(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
      List<Category> list=  categoryMapper.findListByParentId(id);
        if (CollectionUtils.isEmpty(list)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }else {
            return ResponseResult.okResult(list);
        }
    }

    @Override
    public ResponseResult transferGoods(Integer id, Integer prentId) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
       Category category= categoryMapper.findByid(id);
        if (category.getParentId()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_LEVEL_ONE);
        }
        if (category.getParentId()==1){
            categoryMapper.updatePrentIdByid(id,prentId);

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    @Override
    public ResponseResult deleteCategoryByid(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        categoryMapper.updateIsDeleteByid(id);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询所有上级分类
     * @return
     */
    @Override
    public ResponseResult findCategoryNameByParentId() {
        List<CategoryParent> list=categoryMapper.findCategoryNameByParentId();
        return ResponseResult.okResult(list);
    }
    /**
     * 查询商品类型
     * @return
     */
    @Override
    public List<UiGoodsAttribute> findAttribute() {


        List<UiGoodsAttributeCategory> goodsAttributeCategories = goodsAttributeCategoryMapper.selectList(null);

        for (UiGoodsAttributeCategory goodsAttributeCategory : goodsAttributeCategories) {
         List<UiGoodsAttribute>  list=  goodsAttributeMapper.findGoodsAttributeCategoryId(goodsAttributeCategory.getId());
            return list;
        }

        return null;
    }



}
